<?php

class topic extends CI_Controller {

    function __Construct() {
        parent::__Construct();
        $this->load->database();
        $this->load->model('topic_m');
        $this->load->library('form_validation');
        $this->load->library("pagination");
        $this->load->helper('url', 'form');
    }

    function index() {

        $this->data['all_topic'] = $this->topic_m->get();
        $this->example1();
        //$this->load->view('admin/topic/index',$this->data);
    }

    public function topic() {
        $this->data['all_topic'] = $this->topic_m->get();
        $this->example1();
    }

    public function edit($id = NULL) {
        if ($id) {
            $this->data['topic'] = $this->topic_m->get($id);
            count($this->data['topic']) || $this->data['errors'] = 'topic could not be found';
        } else {
            $this->data['topic'] = $this->topic_m->get_new_topic();
        }

        //set up rule
        $rules = $this->topic_m->rules_topic;
        $this->form_validation->set_rules($rules);
        if ($this->form_validation->run() == TRUE) {

            $data = $this->topic_m->array_from_post(
                    array('topic_title', 'topic_content', 'topic_dated', 'topic_author', 'topic_status')
            );
            $data['topic_author'] = 50;
            $this->topic_m->save($data, $id);
            redirect('admin/topic');
        }
        $this->load->view('admin/topic/topic_edit', $this->data);
    }

    public function delete($id) {
        $this->topic_m->delete($id);
        redirect('admin/topic');
    }

    public function example1() {
        $config = array();
        $config["base_url"] = site_url('admin/topic/topic/');
        $config["total_rows"] = $this->topic_m->record_count();
        $config["per_page"] = 4;
        $config["uri_segment"] = 4;



        $this->pagination->initialize($config);

        $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

        $this->data["results"] = $this->topic_m->fetch_topic($config["per_page"], $page);
        $this->data["links"] = $this->pagination->create_links();
        $this->load->view('admin/topic/index', $this->data);
    }

}

?>